package com.phone.handle;

import com.phone.entity.History;
import com.phone.exception.ErrorOwneridException;
import com.phone.exception.NoBindingIdException;
import org.hibernate.Session;
import sun.misc.OSEnvironment;

import java.util.Date;
import java.util.List;

/**
 * Created by tisen on 2017/7/5.
 */
public class HistoryHandle {
    private Session session;

    public HistoryHandle(Session session) {
        this.session = session;
    }

    public void setSession(Session session) {
        this.session = session;
    }

    public Session getSession() {
        return session;
    }


    public String save(String ownerid, int point, float money, String info, int type) throws Exception {
        if (!new UserHandle(session).checkId(ownerid)) {
            throw new ErrorOwneridException();
        }else {
            History history = new History();
            history.setOwnerid(ownerid);
            history.setPoint(point);
            history.setType(type);
            history.setMoney(money);
            history.setDate(new Date());
            history.setInfo(info);
            return (String) session.save(history);
        }
    }

    public List<History> search(String ownerid) throws Exception {
        if(!new UserHandle(session).checkId(ownerid)){
            throw new ErrorOwneridException();
        }
        return session.createQuery("from History where ownerid='" + ownerid + "'").list();
    }

    public History get(String id) throws Exception {
        History history = session.get(History.class, id);
        if (history == null)
            throw new NoBindingIdException();
        return history;
    }
}
